package com.xiaodou.mjf.application.merchant.service.impl;

import javax.annotation.Resource;
import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;

import com.xiaodou.mjf.application.merchant.repository.MerchantRepository;
import com.xiaodou.mjf.application.merchant.service.MerchantService;
import com.xiaodou.mjf.entity.merchant.Merchant;
import com.xiaodou.mjf.infrastructure.common.app.JpaSimpleService;

/***
 *
 * Merchant业务访问实现
 * 
 * @author XiaoDou.com Code Generator
 * @version 1.0 2014-10-29 17:37:52
 *
 */
@Service
@Transactional
public class MerchantServiceImpl extends JpaSimpleService<Merchant, String> implements MerchantService {

	Logger logger = LoggerFactory.getLogger(MerchantServiceImpl.class);

	@Resource
	MerchantRepository merchantRepository;

	@Override
	protected JpaRepository<Merchant, String> getRepository() {
		return merchantRepository;
	}

	@Override
	public Page<Merchant> page(Merchant entity) {
		return merchantRepository.findAll((root, query, cb) -> {
			Predicate p1 = cb.like(root.get("code").as(String.class), "%" + entity.getCode() + "%");

			return query.where(p1).getRestriction();
		}, preparePage(entity, new Sort(new Sort.Order(Sort.Direction.ASC, "code"), new Sort.Order(Sort.Direction.DESC, "name"))));
	}
}
